﻿using Dapper;
using System.Data.SqlClient;
using yunbo.Model.PPMS;

namespace yunbo.Repositorys.PPMSRepositorys
{
    public class Scdd_CpRepoditory
    {
        public string data = "Data Source=121.37.93.71;Initial Catalog=FanXingMES;User ID=sa;Password=geili@2025;";

        public List<Scdd_Cp> GetScdd_Cp(string scdd_code)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"select * from[dbo].[scdd_cp]  where scdd_code='{scdd_code}'";
                List<Scdd_Cp> mes_SCDD = connection.Query<Scdd_Cp>(sql).ToList();
                return mes_SCDD;
            }
        }

        public int AddScdd_Cp(Scdd_Cp scdd_cp)
        {
            string sql = @"INSERT INTO scdd_cp(scdd_code, cp_code, cp_name, cp_xh, cp_lx,cp_fl,pp_code, dw_code,dw_name, bom_code, cp_shu)VALUES (@scdd_code, @cp_code, @cp_name, @cp_xh, @cp_lx,@cp_fl,@pp_code, @dw_code, @dw_name, @bom_code, @cp_shu)";

            try
            {
                using (SqlConnection connection = new SqlConnection(data))
                {
                    connection.Open();
                    var parameters = new
                    {
                        scdd_code = scdd_cp.scdd_code,
                        cp_code = scdd_cp.cp_code,
                        cp_name = scdd_cp.cp_name,
                        cp_xh = scdd_cp.cp_xh,
                        cp_lx = scdd_cp.cp_lx,
                        cp_fl= scdd_cp.cp_fl,
                        pp_code= scdd_cp.pp_code,
                        dw_code = scdd_cp.dw_code,
                        dw_name = scdd_cp.dw_name,
                        bom_code = scdd_cp.bom_code,
                        cp_shu = scdd_cp.cp_shu,
                    };

                    var affectedRows = connection.Execute(sql, parameters);
                    return affectedRows;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }

        public int UpdateScdd_Cp(Scdd_Cp scdd_cp)
        {
            string sql = @"UPDATE scdd_cp SET cp_name = @cp_name,cp_xh = @cp_xh,cp_lx = @cp_lx,cp_fl = @cp_fl,pp_code = @pp_code,dw_code = @dw_code,dw_name = @dw_name,bom_code = @bom_code,cp_shu = @cp_shu WHERE scdd_code = @scdd_code and cp_code = @cp_code";

            try
            {
                using (SqlConnection connection = new SqlConnection(data))
                {
                    connection.Open();
                    var parameters = new
                    {
                        scdd_code = scdd_cp.scdd_code,
                        cp_code = scdd_cp.cp_code,
                        cp_name = scdd_cp.cp_name,
                        cp_xh = scdd_cp.cp_xh,
                        cp_lx = scdd_cp.cp_lx,
                        cp_fl = scdd_cp.cp_fl,
                        pp_code = scdd_cp.pp_code,
                        dw_code = scdd_cp.dw_code,
                        dw_name = scdd_cp.dw_name,
                        bom_code = scdd_cp.bom_code,
                        cp_shu = scdd_cp.cp_shu,
                    };

                    var affectedRows = connection.Execute(sql, parameters);
                    return affectedRows;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
    }
}
